package com.example.yls.demoa;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Point;
import android.icu.util.Measure;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by yls on 2018/6/13.
 */

public class wuziqiView extends View {
    private int mPanelWidth;
    private  float mLineHegiht;
    private int MAX_LINE=10;
    private int MAX_LINE_COUNT=5;
 private  float   rariopieceofLineHeight=3*1.0f/4;
    private Paint mPaint=new Paint();
    private Bitmap mWithepicec,mBlackpicec;
    //判断白
    private boolean is_White=true;
    private ArrayList<Point> mWhiteArray=new ArrayList<>();
    private ArrayList<Point> mBlackArray=new ArrayList<>();
    private String TAG="TAG";
    private boolean misgameover,misWhiteWinner;



    public wuziqiView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        setBackgroundColor(0x44ff00);
        init();
    }

    private void init() {
        mPaint.setColor(0x88000000);
        mPaint.setAntiAlias(true);
        mPaint.setDither(true);
        mPaint.setStyle(Paint.Style.FILL);
        mWithepicec= BitmapFactory.decodeResource(getResources(),R.drawable.stone_w2);
        mBlackpicec=BitmapFactory.decodeResource(getResources(),R.drawable.stone_b1);

    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
      int widthSize=  MeasureSpec.getSize(widthMeasureSpec);
      int widthmodel=MeasureSpec.getMode(widthMeasureSpec);

      int heightSize=MeasureSpec.getSize(heightMeasureSpec);
      int heightmodel=MeasureSpec.getMode(heightMeasureSpec);

    int width=  Math.min(widthSize,heightSize);
    if (widthmodel== MeasureSpec.UNSPECIFIED){
        width=heightSize;
    }
        if (heightmodel== MeasureSpec.UNSPECIFIED){
            width=widthSize;
        }
    setMeasuredDimension(width,width);

    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        mPanelWidth=w;
        mLineHegiht=mPanelWidth*1.0f/MAX_LINE;
        Log.e("TAG", "onSizeChanged:LH "+mLineHegiht );
        int WidthPicec= (int) (mLineHegiht*rariopieceofLineHeight);
        Log.e(TAG, "onSizeChanged: wp"+WidthPicec );
        mWithepicec=Bitmap.createScaledBitmap(mWithepicec,WidthPicec,WidthPicec,false);
        mBlackpicec=Bitmap.createScaledBitmap(mBlackpicec,WidthPicec,WidthPicec,false);


    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        drawBorad(canvas);
        drawPicec(canvas);
        checkGameOver();
        
    }

    private void checkGameOver() {
      boolean wihtewin=  checkFiveInLine(mWhiteArray);
        boolean blackwin=  checkFiveInLine(mBlackArray);
        if (wihtewin||blackwin){
            misgameover=true;
            misWhiteWinner=wihtewin;
            String text=misWhiteWinner?"白棋胜":"黑棋胜";
            Toast.makeText(getContext(), text, Toast.LENGTH_SHORT).show();
        }
    }

    private boolean checkFiveInLine(List<Point> points) {
        for(Point p:points){
            int x=p.x;
            int y=p.y;
            boolean win=checkHorizontal(x,y,points);
            if (win) return true;
            win=checkVertical(x,y,points);
            if (win) return true;
            win=checkleftDia(x,y,points);
            if (win) return true;
            win=checkrightDia(x,y,points);
            if (win) return true;
        }
        return false;
    }
//横向判定
    private boolean checkHorizontal(int x, int y, List<Point> points) {
        int count=1;
       //左
        for (int i=1;i<MAX_LINE_COUNT;i++){
           if (points.contains(new Point(x-i,y))){
               count++;
           }else {
               break;
           }
        }
        if (count==MAX_LINE_COUNT){
            return true;
        }
        //右
        for (int i=1;i<MAX_LINE_COUNT;i++){
            if (points.contains(new Point(x+i,y))){
                count++;
            }else {
                break;
            }
        }
        if (count==MAX_LINE_COUNT){
            return true;
        }


        return false;
    }

    //纵向判定
    private boolean checkVertical(int x, int y, List<Point> points) {
        int count=1;
        //上
        for (int i=1;i<MAX_LINE_COUNT;i++){
            if (points.contains(new Point(x,y-i))){
                count++;
            }else {
                break;
            }
        }
        if (count==MAX_LINE_COUNT){
            return true;
        }
        //下
        for (int i=1;i<MAX_LINE_COUNT;i++){
            if (points.contains(new Point(x,y+i))){
                count++;
            }else {
                break;
            }
        }
        if (count==MAX_LINE_COUNT){
            return true;
        }


        return false;
    }

    //左斜判定
    private boolean checkleftDia(int x, int y, List<Point> points) {
        int count=1;
        //左
        for (int i=1;i<MAX_LINE_COUNT;i++){
            if (points.contains(new Point(x-i,y+i))){
                count++;
            }else {
                break;
            }
        }
        if (count==MAX_LINE_COUNT){
            return true;
        }
        //右
        for (int i=1;i<MAX_LINE_COUNT;i++){
            if (points.contains(new Point(x+i,y-i))){
                count++;
            }else {
                break;
            }
        }
        if (count==MAX_LINE_COUNT){
            return true;
        }


        return false;
    }
    //右斜判定
    private boolean checkrightDia(int x, int y, List<Point> points) {
        int count=1;
        //左
        for (int i=1;i<MAX_LINE_COUNT;i++){
            if (points.contains(new Point(x-i,y-i))){
                count++;
            }else {
                break;
            }
        }
        if (count==MAX_LINE_COUNT){
            return true;
        }
        //右
        for (int i=1;i<MAX_LINE_COUNT;i++){
            if (points.contains(new Point(x+i,y+i))){
                count++;
            }else {
                break;
            }
        }
        if (count==MAX_LINE_COUNT){
            return true;
        }


        return false;
    }
    private void drawPicec(Canvas canvas) {
        for (int i=0,n=mWhiteArray.size();i<n;i++){
            Point wPoint=mWhiteArray.get(i);
            canvas.drawBitmap(mWithepicec,(wPoint.x+(1-rariopieceofLineHeight)/2)*mLineHegiht,
                    (wPoint.y+(1-rariopieceofLineHeight)/2)*mLineHegiht,null
                    );
        }
        for (int i=0,n=mBlackArray.size();i<n;i++){
            Point bPoint=mBlackArray.get(i);
            canvas.drawBitmap(mBlackpicec,(bPoint.x+(1-rariopieceofLineHeight)/2)*mLineHegiht,
                    (bPoint.y+(1-rariopieceofLineHeight)/2)*mLineHegiht,null
            );
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (misgameover){
            return false;}
        int action = event.getAction();
        if (action==MotionEvent.ACTION_UP){
            int x= (int) event.getX();
            int y= (int) event.getY();
            Point point=getValidPoint(x,y);
            if (mWhiteArray.contains(point)||mBlackArray.contains(point)){
                return false;
            }
            if (is_White){
                mWhiteArray.add(point);
            }else{
                mBlackArray.add(point);
            }
            invalidate();
            is_White=!is_White;
            return true;
        }
 return  true;
    }

    private Point getValidPoint(int x, int y) {
        return new Point((int) (x/mLineHegiht),(int) (y/mLineHegiht));
    }

    private void drawBorad(Canvas canvas) {
        int w=mPanelWidth;
        float lineHegiht=mLineHegiht;
        for (int i=0;i<MAX_LINE;i++){
            int StartX= (int) (lineHegiht/2);
            int endx= (int) (w-lineHegiht/2);
            int y= (int) ((0.5+i)*lineHegiht);
            canvas.drawLine(StartX,y,endx,y,mPaint);
            canvas.drawLine(y,StartX,y,endx,mPaint);
        }
    }
    public  void start(){
        mWhiteArray.clear();
        mBlackArray.clear();
        misgameover=false;
        misWhiteWinner=false;
        invalidate();
    }
    private static final String INSTANCE="instance";
    private static final String INSTANCE_GAME_OVER="instance_game_over";
    private static final String INSTANCE_WHITE_ARRY="instance_WHITE_ARRY";
    private static final String INSTANCE_BLACK_ARRY="instance_BLACK_ARRY";

    @Nullable
    @Override
    protected Parcelable onSaveInstanceState() {
        Bundle bundle=new Bundle();
        bundle.putParcelable(INSTANCE,super.onSaveInstanceState());
        bundle.putBoolean(INSTANCE_GAME_OVER,misgameover);
        bundle.putParcelableArrayList(INSTANCE_WHITE_ARRY,mWhiteArray);
        bundle.putParcelableArrayList(INSTANCE_BLACK_ARRY,mBlackArray);
        return bundle;
    }

    @Override
    protected void onRestoreInstanceState(Parcelable state) {
        if (state instanceof Bundle){
            Bundle bundle= (Bundle) state;
            misgameover=bundle.getBoolean(INSTANCE_GAME_OVER);
            mWhiteArray=bundle.getParcelableArrayList(INSTANCE_WHITE_ARRY);
            mBlackArray=bundle.getParcelableArrayList(INSTANCE_BLACK_ARRY);
            super.onRestoreInstanceState(bundle.getParcelable(INSTANCE));
            return;
        }

        super.onRestoreInstanceState(state);
    }
}
